﻿
var jqGridHelper = {
    /** 常用配置 */ 
    getConfig: function (pageId, h, c) {
        ///    <summary>
        ///    返回jqGrid的通用配置项
        ///    <summary>
        ///    <returns type="Object" />
        ///	<param name="divPageId" type="String">
        ///		Grid的分页divID，含"#"
        ///	</param>
        ///	<param name="h" type="Number">
        ///		Grid的高
        ///	</param>
        ///	<param name="c" type="Object">
        ///		Grid的非通用配置项
        ///	</param>
        //分页设置
        $(pageId).jqGrid(
                    'navGrid',
                    pageId,
                    { del: false, add: false, edit: false },
                    {}, {}, {},
                    { multipleSearch: true });
      var config = {
            datatype: "json",
            mtype: "post",
            height: h,
            //暂进不使用滚动条分页
            //scroll: true,
            pager: pageId,
            loadonce: false,
            rownumbers: true,
            rownumWidth: 30,
            gridview: false,
            viewrecords: true,
            sortorder: "asc",
            shrinkToFit: false,
            autowidth: true,
            altRows: true,
            altclass: 'ui-state-disabled', //隔行变色样式
            jsonReader: { repeatitems: false }
        }
        return jQuery.extend(config, c);
    },



    //*** 相关的辅助方法--开始****//

    addRow: function (gridId, data) {
        ///    <summary>
        ///    让Grid最后添加一行，并且直接进入编辑模式
        ///    <summary>
        ///    <returns type="Bool" />
        ///	<param name="gridId" type="String">
        ///		Grid的ID，含"#"
        ///	</param>
        ///	<param name="data" type="Object">
        ///		新数据
        ///	</param>
        var rowCount = $(gridId).getDataIDs().length + 1;
        $(gridId).jqGrid('addRowData', rowCount, data);
        $(gridId).jqGrid('editRow', rowCount, data);
        return rowCount;
    },

    getAllRowDataHtml: function (gridId) {
        ///     <summary>
        ///     让Grid最后添加一行，并且直接进入编辑模式
        ///    <summary>
        ///	<returns type="String" />
        ///	<param name="gridId" type="String">
        ///		Grid的ID，含"#"
        ///	</param>
        var ids = $(gridId).getDataIDs();
        var data = $(gridId).getRowData(ids[0]);
        var colNames = new Array();
        var ii = 0;
        for (var i in data) { colNames[ii++] = i; }
        var html = "";

        for (i = 0; i < ids.length; i++) {
            $(gridId).saveRow(ids[i], false, 'clientArray');
            data = $(gridId).getRowData(ids[i]);
            for (j = 0; j < colNames.length; j++) {
                html = html + data[colNames[j]] + "\t";
            }
            html = html + "\n";
        }
        html = html + "\n";
        jqGridHelper.editAllRow(gridId);
        return html;
    },

    getAllRowData: function (gridId) {
        ///     <summary>
        ///     让Grid最后添加一行，并且直接进入编辑模式
        ///    <summary>
        ///	<returns type="Array" />
        ///	<param name="gridId" type="String">
        ///		Grid的ID，含"#"
        ///	</param>
        var ids = $(gridId).getDataIDs();
        var data = $(gridId).getRowData(ids[0]);
        var dataList = new Array();
        for (i = 0; i < ids.length; i++) {
            $(gridId).saveRow(ids[i], false, 'clientArray');
            data = $(gridId).getRowData(ids[i]);
            dataList[i] = data;
        }
        jqGridHelper.editAllRow(gridId);
        return dataList;
    },

    editAllRow: function (gridId) {
        ///     <summary>
        ///     让Grid的所有行进入编辑模式
        ///    <summary>
        ///	<param name="gridId" type="String">
        ///		Grid的ID，含"#"
        ///	</param>
        var ids = $(gridId).jqGrid('getDataIDs');
        $(ids).each(function (i, d) {
            if (i > 0)
                $(gridId).jqGrid('editRow', d, true);
        });
        $(gridId).jqGrid('editRow', ids[0], true);
        $(gridId).jqGrid('setSelection', ids[0]);
    },

    setMaxWidth: function (parentDivId, w) {
        ///     <summary>
        ///     设置Grid的最大宽
        ///    <summary>
        ///	<param name="parentDivId" type="String">
        ///		Grid父元素的ID，不含"#"
        ///	</param>
        ///	<param name="w" type="double">
        ///		最大宽
        ///	</param>
        //        var divIds = ["gbox_", "lui_", "gview_", "t_"];
        //        $(divIds).each(function (i, d) {
        //            $("#" + d + gridId).width(w);
        //        });
        //        $("#divHTScrool").width(w);
        //        $("#pg_divHTScrool").width(w);
                return;
        $("#" + parentDivId + " div").each(function () {
            if ($(this).width() >= w) {

                $(this).width(w);
                //if ($(this).attr("id").length < 1) debugger;
                //alert($(this).attr("id"));
                alert($(this).attr("id") + "    " + $(this).attr("class")) 
            }
        });
    },
    //*** 相关的辅助方法--结束****//


    //*** 关于自定义编辑框--开始****//

    dateElement: function (value, options) {
        ///	<summary>
        ///		Grid中的日期编辑框，创建对象
        ///	</summary>
        var el = document.createElement("input");
        $(el).attr("type", "text").css("width","100%").val(value);
        $(el).click(WdatePicker);
        return el;
    },
    dateValue: function (el) {
        ///	<summary>
        ///		Grid中的日期编辑框，返回值
        ///	</summary>
        return $(el).val();
    }
    //*** 关于自定义编辑框--结束****//

}

//*** 关于自定义Fmatter--开始****//

//是否--10
jQuery.extend($.fn.fmatter, {
    YNFmatter: function (cellvalue, options, rowdata) {
        var val = "";
        if (cellvalue == "1") val = "是";
        if (cellvalue == "0") val = "否";
        return val;
    }
});
jQuery.extend($.fn.fmatter.YNFmatter, {
    unformat: function (cellvalue, options) {
        var val = "";
        if (cellvalue == "是") val = "1";
        if (cellvalue == "否") val = "0";
        return val;
    }
});

//处理百分比
jQuery.extend($.fn.fmatter, {
    percentFmatter: function (cellvalue, options, rowdata) {
        return Math.round(cellvalue * 100) / 100 + "%";
    }
});
jQuery.extend($.fn.fmatter.percentFmatter, {
    unformat: function (cellvalue, options) {
        var val = cellvalue.replace("%", "");
        return parseFloat(val) / 100;
    }
});

//处理日期型数据 2011-3-23 段松波
jQuery.extend($.fn.fmatter, {
    cDate: function (cellvalue, options) {
        if ((typeof (cellvalue) == "undefined") || cellvalue == null) return "";
        else {
            try {
                return (new Date(parseInt(cellvalue.replace("/Date(", "").replace(")/", "")))).format("yyyy-MM-dd")
            } catch (e) {
                return "";
            }
        }
    }
});
//经营模式格式化 2011-3-31 XHY （如果从数据字典中读出，则此函数可以删除）
jQuery.extend($.fn.fmatter, {
    TypeFmatter: function (cellvalue, options, rowdata) {
        var val = "";
        if (cellvalue == "0") val = "制造型";
        if (cellvalue == "1") val = "贸易型";
        if (cellvalue == "2") val = "服务型";
        return val;
    }
});

//处理JQuery里单元格中的超链接,目前用在单位信息里面,算定义entity单元格值为链接地址
jQuery.extend($.fn.fmatter, {
    LinkFormat: function (cellvalue, options) {
        if ((typeof (cellvalue) == "undefined") || cellvalue == null) return "#";
        else {
            var linkContent = "";
            if (options.type == "1")
                linkContent = "//BaseInfo/CompanyInfo/LookCompany/" + cellvalue;
            var htmlLink = "<a href=\"" + linkContent + "\" target=\"dialog\" title=\"查看详细单位信息\" mask=\"true\"><span>查看</span></a>"
        }
    }
});
//*** 关于自定义Fmatter--结束****//
